<?php
namespace Home\Controller;
use Think\Controller;
class UserController extends Controller {
	public function login(){
		$this->display();
	}
	public function do_login(){
		$rules = array(
			array('verify','require','验证码必须！'),
			array('username','require','账户名必须！'),
			array('password','require','密码必须！'),
		);
		$User = M("user_login");
		if (!$User->validate($rules)->create()){
			$this->error($User->getError());
		}else{
			$Verify = new \Think\Verify();
			if (!$Verify->check(I('post.verify'))){
				$this->error('验证码不正确！');
			}
			$User = M("user_login")->where(array('username' => I('post.username'),'password' => md5(I('post.password'))))->find();
			if (!$User) {
				$this->error('账户名和密码不正确！');
			}
			$_SESSION['user_is_login']=true;
			$_SESSION['user_name']=$User['username'];
			$_SESSION['user_id']=$User['id'];
			$this->redirect('front/index');
		}
	}
	public function regedit(){
		$this->display();
	}
	public function do_regedit(){
		$rules = array(
			array('verify','require','验证码必须！'),
			array('username','require','账户名必须！'),
			array('password','require','密码必须！'),
			array('repassword','password','确认密码不正确',0,'confirm'),
		);
		$User = M("user_login");
		if (!$User->validate($rules)->create()){
			$this->error($User->getError());
		}else{
			$Verify = new \Think\Verify();
			if (!$Verify->check(I('post.verify'))){
				$this->error('验证码不正确！');
			}
			$User = M("user_login")->where('username ='. I('post.username'))->find();
			if ($User) {
				$this->error('账户名已存在！');
			}
			$User = M("user_login");
			$data['username']=I('post.username');
			$data['password']=md5(I('post.password'));
			$User->add($data);
			$this->success('注册成功，请登录','login');
		}
	}
	public function verify(){
		$Verify = new \Think\Verify();
		$Verify->length=3;
		$Verify->entry();
	}
}